package com.ebupt.migu.music.effectStatistics.service.Impl;

import com.ebupt.migu.music.effectStatistics.entity.WeeklyTotalBean;
import com.ebupt.migu.music.effectStatistics.mapper.WeeklyTotalMapper;
import com.ebupt.migu.music.effectStatistics.service.WeeklyTotalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tz
 */
@Slf4j
@Service
public class WeeklyTotalServiceImpl implements WeeklyTotalService {

    @Autowired
    private WeeklyTotalMapper weeklyTotalMapper;

    @Override
    public List<String> export(Map<String, Object> params) {
        //时间参数替换
        params.put("startTimeStr",params.get("startTime").toString().replaceAll("-",""));
        params.put("endTimeStr",params.get("endTime").toString().replaceAll("-",""));
        //结果集
        List<String> list = new LinkedList<>();
        //初始化字段map
        Map<String,Object> strMap = beanToMap(new WeeklyTotalBean());
        //结果集
        Map<String,Object> reMap = new HashMap<>();
        //人群创建个数
        List<Map<String, Object>> userList = weeklyTotalMapper.qryUserGroup(params);
        for(Map<String,Object> map : userList){
            switch (map.get("createWay").toString()) {
                case "2":
                    reMap.put("tagChoose",map.get("num"));
                    break;
                case "3":
                    reMap.put("ftpInput",map.get("num"));
                    break;
                case "5":
                    reMap.put("userGroupRe",map.get("num"));
                    break;
                case "6":
                    reMap.put("userGroupInput",map.get("num"));
                    break;
                default:
                    break;
            }
        }
        //人群失败
        reMap.putAll(weeklyTotalMapper.qryFailUser(params));
        //短信运营
        reMap.putAll(weeklyTotalMapper.qrySms(params));
        //push
        reMap.putAll(weeklyTotalMapper.qryPush1(params));
        reMap.putAll(weeklyTotalMapper.qryPush2(params));
        //消息通知
        reMap.putAll(weeklyTotalMapper.qryMsg(params));
        //组合数据
        for(Map.Entry<String,Object> str : strMap.entrySet()){
            for(Map.Entry<String,Object> re : reMap.entrySet()){
                if(str.getKey().equals(re.getKey())){
                    list.add(str.getValue()+":"+re.getValue().toString());
                }
            }
        }
        //自动触发
        List<Map<String, Object>> autoList = weeklyTotalMapper.qryAutoTri(params);
        list.addAll(autoList.stream().map(e -> e.get("triSceneDetail").toString()
                +"运营用户数:"+e.get("autotriNum")).collect(Collectors.toList()));
        list.addAll(autoList.stream().map(e -> e.get("triSceneDetail").toString()
                +"平均转化率:"+e.get("autotriAvg")).collect(Collectors.toList()));
        return list;
    }

    /**
     * JavaBean转换Map
     *
     * @param object
     * @return
     */
    public static Map<String, Object> beanToMap(Object object) {
        Map<String, Object> result = new HashMap<>();
        //获得类的的属性名数组
        Field[] fields = object.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                String name = new String(field.getName());
                result.put(name, field.get(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
